local lizhan = fk.CreateSkill {
  name = "lingling__lizhan",
}

Fk:loadTranslationTable{
  ["lingling__lizhan"] = "励战",
  [":lingling__lizhan"] = "游戏开始时和回合结束时，你可以令任意名角色各获得一张【闪】，以此法获得的【闪】使用时，使用者判定，黑色则无效。",

  ["#lingling__lizhan-invoke"] = "励战：你可以令任意名角色各获得一张【闪】，可能不一定好用",

  ["@@lingling__lizhan"] = "励战",
}

local spec = {
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getAlivePlayers(false)
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = #targets,
      skill_name = lizhan.name,
      prompt = "#lingling__lizhan-invoke"
    })
    if #tos > 0 then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    for _, to in ipairs(tos) do
      room:obtainCard(to, room:getCardsFromPileByRule("jink", 1, "allPiles"), nil, fk.ReasonJustMove, player, lizhan.name, "@@lingling__lizhan")
    end
  end
}

lizhan:addEffect(fk.GameStart, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(lizhan.name)
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

lizhan:addEffect(fk.TurnEnd, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(lizhan.name) and target == player
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

lizhan:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  mute = true,
  can_refresh = function(self, event, target, player, data)
    local remove = {}
    for _, move in ipairs(data) do
      if move.from == nil or move.toArea ~= Card.Processing then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card:getMark("@@lingling__lizhan") ~= 0 and table.contains({ Card.Processing, Card.PlayerHand }, info.fromArea) then
            table.insert(remove, info.cardId)
          end
        end
      end
    end
    if #remove > 0 then
      event:setCostData(self, {cards = remove})
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local remove = table.simpleClone(event:getCostData(self).cards)
    for _, id in ipairs(remove) do
      room:setCardMark(Fk:getCardById(id), "@@lingling__lizhan", 0)
    end
  end
})
lizhan:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.card and data.card:getMark("@@lingling__lizhan") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = { ---@type JudgeDataSpec
      who = player,
      pattern = ".|.|heart,diamond",
      reason = lizhan.name
    }
    room:judge(judge)
    if judge.card and judge.card.color == Card.Black then
      data.toCard = nil
    end
  end
})

return lizhan
